package com.yuerlive.rainbow.module.playlive.adapter;

import android.content.Context;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.yuerlive.rainbow.R;
import com.yuerlive.rainbow.entity.Gift;
import com.yuerlive.rainbow.utils.DeviceUtils;
import com.yuerlive.rainbow.utils.GlideUtil;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Admin on 2017/5/24.
 */

public class GiftAdapter extends BaseAdapter {
    private Context context;
    private List<Gift> gifts;
    private LayoutInflater inflater;
    private ItemOnClickListener listener;

    private int height;

    public GiftAdapter(Context context, List<Gift> gifts, int height) {
        this.context = context;
        this.gifts = gifts;
        inflater = LayoutInflater.from(context);
        this.height = height;
    }

    @Override
    public int getCount() {
        if (gifts != null && gifts.size() != 0) {
            return gifts.size();
        }
        return 0;
    }

    @Override
    public Object getItem(int position) {
        return gifts.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.layout_pager_gift_item, null);
            holder = new ViewHolder(convertView);
            ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
                    ViewGroup.LayoutParams.WRAP_CONTENT);
            params.height = height;
            convertView.setLayoutParams(params);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        setData(holder, position);
        return convertView;
    }

    private void setData(ViewHolder holder, int position) {
        Gift gift = gifts.get(position);
        if (gift == null) {
            return;
        }
        holder.tvGiftPrice.setText(gift.getGiftRainbowSweet() + "");
        holder.tvGiftName.setText(gift.getName());
        //   GlideUtil.load(mContext,"http://",imgIcon);
        holder.imgIcon.setTag(gift.getId());
        if (gift.getChoosed() == 1) {
            holder.rlParent.setBackgroundResource(R.drawable.bg_retangle_01);
        } else {
            holder.rlParent.setBackgroundColor(context.getResources().getColor(R.color.black_percent_50));
        }
        listener.setView(holder.rlParent);
        GlideUtil.load(context, gift.getIcon(), holder.imgIcon);
        if (gift.getType() == 2) {  //
            holder.ivGiftType.setVisibility(View.VISIBLE);
            holder.ivGiftType.setImageResource(R.drawable.gift_l);
        } else if (gift.getType() == 3) {
            holder.ivGiftType.setVisibility(View.VISIBLE);
            holder.ivGiftType.setImageResource(R.drawable.gift_p);
        } else {
            holder.ivGiftType.setVisibility(View.GONE);
        }
//        ViewGroup.LayoutParams layoutParams=(ViewGroup.LayoutParams)holder.rlParent.getLayoutParams();
//        layoutParams.height= DeviceUtils.deviceWidth(context)/4;
//         holder.rlParent.setLayoutParams(layoutParams);
        holder.rlParent.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                listener.itemClick(position, v);
            }
        });
    }

    static class ViewHolder {
        @BindView(R.id.imgIcon)
        ImageView imgIcon;
        @BindView(R.id.ivGiftType)
        ImageView ivGiftType;
        @BindView(R.id.tvGiftPrice)
        TextView tvGiftPrice;
        @BindView(R.id.tvGiftName)
        TextView tvGiftName;
        @BindView(R.id.rlParent)
        RelativeLayout rlParent;

        public ViewHolder(View convertView) {
            ButterKnife.bind(this, convertView);
        }
    }

    public interface ItemOnClickListener {
        void itemClick(int position, View view);

        void setView(View view);
    }

    public void setItemClickListener(ItemOnClickListener listener) {
        this.listener = listener;
    }
}
